<?php

namespace app\common\model\mall\order;

use think\Model;

class Invoice extends Model
{
    // 表名
    protected $name = 'mall_order_invoice';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = "int";

    // 定义时间戳字段名
    protected $createTime = "createtime";
    protected $updateTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'status_text'
    ];

    public function getTypeList()
    {
        return ['0' => __('Type 0'),'1' => __('Type 1')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'),'hidden' => __('Hidden')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /**
     * 获取发票信息【存入订单表字段】
     */
    public function getInviceInfo($id,$uId=0){
        if($id==0)return json_encode(['invoiceHead'=>'个人']);// 所需发票为个人时
        $userId = ($uId==0)?(int)cookie('uid'):$uId;
        $rs = $this->where(['id'=>$id,'user_id'=>$userId,'status'=>"normal"])->find();
        if(empty($rs))return [];
        $jsonArr = [];
        $jsonArr['type'] = 0;//0:纸质发票 1:电子发票【后续扩展】
        $jsonArr['invoiceHead'] = $rs['invoiceHead'];
        $jsonArr['invoiceCode'] = $rs['invoiceCode'];
        $jsonArr['id'] = $rs['id'];
        return json_encode($jsonArr);
    }

}
